package org.jiuling.aliyun.sms.convert;

import org.jiuling.aliyun.config.exception.AliYunConfigRuntimeException;
import org.jiuling.aliyun.sms.enums.TemplateTypeEnum;
import org.jiuling.aliyun.sms.model.QuerySmsTemplateListResponseModel;
import org.jiuling.aliyun.sms.model.enums.AuditStatusEnum;
import org.jiuling.basics.converter.DateConvert;
import org.jiuling.basics.enums.DateTimeFormatEnum;
import org.jiuling.basics.exception.BasicsException;
import org.jiuling.basics.util.EnumUtil;
import org.jiuling.basics.validation.ListVerify;
import org.jiuling.basics.validation.ObjectVerify;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询短信模板列表响应转换
 * @author 紫灵
 * @date 2023年02月12日 22时48分15秒
 */
public class QuerySmsTemplateListResponseConvert {

    /** 私有构造方法，禁止创建对象 */
    private QuerySmsTemplateListResponseConvert(){};

    /**
     * 内容转模型
     * @param querySmsTemplateListResponseBody 查询短信模板列表响应内容
     * @return 查询短信模板列表响应模型
     */
    public static QuerySmsTemplateListResponseModel bodyToModel(com.aliyun.dysmsapi20170525.models.QuerySmsTemplateListResponseBody querySmsTemplateListResponseBody){
        QuerySmsTemplateListResponseModel querySmsTemplateListResponseModel = new QuerySmsTemplateListResponseModel();

        querySmsTemplateListResponseModel.setTotalCount(querySmsTemplateListResponseBody.getTotalCount());
        querySmsTemplateListResponseModel.setCurrentPage(querySmsTemplateListResponseBody.getCurrentPage());
        querySmsTemplateListResponseModel.setPageSize(querySmsTemplateListResponseBody.getPageSize());
        querySmsTemplateListResponseModel.setCode(querySmsTemplateListResponseBody.getCode());
        querySmsTemplateListResponseModel.setMessage(querySmsTemplateListResponseBody.getMessage());
        querySmsTemplateListResponseModel.setRequestId(querySmsTemplateListResponseBody.getRequestId());

        // 封装短信模板
        List<com.aliyun.dysmsapi20170525.models.QuerySmsTemplateListResponseBody.QuerySmsTemplateListResponseBodySmsTemplateList> smsTemplateBodyList = querySmsTemplateListResponseBody.getSmsTemplateList();
        if(!ListVerify.isEmpty(smsTemplateBodyList)) {
            List<QuerySmsTemplateListResponseModel.SmsTemplate> smsTemplateList = smsTemplateBodyList.stream().map(smsTemplateBody -> {
                QuerySmsTemplateListResponseModel.SmsTemplate smsTemplate = new QuerySmsTemplateListResponseModel.SmsTemplate();
                smsTemplate.setTemplateCode(smsTemplateBody.getTemplateCode());
                smsTemplate.setTemplateName(smsTemplateBody.getTemplateName());
                smsTemplate.setTemplateContent(smsTemplateBody.getTemplateContent());
                smsTemplate.setOrderId(smsTemplateBody.getOrderId());
                try {
                    smsTemplate.setCreateDate(DateConvert.stringToDate(smsTemplateBody.getCreateDate(), DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                    smsTemplate.setOuterTemplateType(EnumUtil.valueOfEnum(TemplateTypeEnum.class, smsTemplateBody.getOuterTemplateType(), "getValue"));
                    smsTemplate.setAuditStatus(EnumUtil.valueOfEnum(AuditStatusEnum.class, smsTemplateBody.getAuditStatus(), "name"));
                    smsTemplate.setTemplateType(EnumUtil.valueOfEnum(TemplateTypeEnum.class, smsTemplateBody.getTemplateType(), "getValue"));
                } catch (BasicsException e) {
                    throw new AliYunConfigRuntimeException(e.getLocalizedMessage());
                }

                // 封装审核备注
                com.aliyun.dysmsapi20170525.models.QuerySmsTemplateListResponseBody.QuerySmsTemplateListResponseBodySmsTemplateListReason reasonBody = smsTemplateBody.getReason();
                if (!ObjectVerify.isNull(reasonBody)) {
                    QuerySmsTemplateListResponseModel.SmsTemplate.Reason reason = new QuerySmsTemplateListResponseModel.SmsTemplate.Reason();
                    if(!ObjectVerify.isNull(reasonBody.getRejectDate())) {
                        try {
                            reason.setRejectDate(DateConvert.stringToDate(reasonBody.getRejectDate(), DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                        } catch (BasicsException e) {
                            throw new AliYunConfigRuntimeException(e.getLocalizedMessage());
                        }
                    }
                    reason.setRejectInfo(reasonBody.getRejectInfo());
                    reason.setRejectSubInfo(reasonBody.getRejectSubInfo());

                    smsTemplate.setReason(reason);
                }
                return smsTemplate;
            }).collect(Collectors.toList());
            querySmsTemplateListResponseModel.setSmsTemplateList(smsTemplateList);
        }
        return querySmsTemplateListResponseModel;
    }

    /**
     * 内容转模型
     * @param querySmsTemplateListResponseBody 查询短信模板列表响应内容
     * @return 查询短信模板列表响应模型
     */
    public static QuerySmsTemplateListResponseModel bodyToModel(com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsTemplateListResponseBody querySmsTemplateListResponseBody){
        QuerySmsTemplateListResponseModel querySmsTemplateListResponseModel = new QuerySmsTemplateListResponseModel();

        querySmsTemplateListResponseModel.setTotalCount(querySmsTemplateListResponseBody.getTotalCount());
        querySmsTemplateListResponseModel.setCurrentPage(querySmsTemplateListResponseBody.getCurrentPage());
        querySmsTemplateListResponseModel.setPageSize(querySmsTemplateListResponseBody.getPageSize());
        querySmsTemplateListResponseModel.setCode(querySmsTemplateListResponseBody.getCode());
        querySmsTemplateListResponseModel.setMessage(querySmsTemplateListResponseBody.getMessage());
        querySmsTemplateListResponseModel.setRequestId(querySmsTemplateListResponseBody.getRequestId());

        // 封装短信模板
        List<com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsTemplateListResponseBody.SmsTemplateList> smsTemplateBodyList = querySmsTemplateListResponseBody.getSmsTemplateList();
        if(!ListVerify.isEmpty(smsTemplateBodyList)){
            List<QuerySmsTemplateListResponseModel.SmsTemplate> smsTemplateList = smsTemplateBodyList.stream().map(smsTemplateBody -> {
                QuerySmsTemplateListResponseModel.SmsTemplate smsTemplate = new QuerySmsTemplateListResponseModel.SmsTemplate();
                smsTemplate.setTemplateCode(smsTemplateBody.getTemplateCode());
                smsTemplate.setTemplateName(smsTemplateBody.getTemplateName());
                smsTemplate.setTemplateContent(smsTemplateBody.getTemplateContent());
                smsTemplate.setOrderId(smsTemplateBody.getOrderId());
                try {
                    smsTemplate.setCreateDate(DateConvert.stringToDate(smsTemplateBody.getCreateDate(), DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                    smsTemplate.setOuterTemplateType(EnumUtil.valueOfEnum(TemplateTypeEnum.class, smsTemplateBody.getOuterTemplateType(), "getValue"));
                    smsTemplate.setAuditStatus(EnumUtil.valueOfEnum(AuditStatusEnum.class, smsTemplateBody.getAuditStatus(), "name"));
                    smsTemplate.setTemplateType(EnumUtil.valueOfEnum(TemplateTypeEnum.class, smsTemplateBody.getTemplateType(), "getValue"));
                }  catch (BasicsException e) {
                    throw new AliYunConfigRuntimeException(e.getLocalizedMessage());
                }

                // 封装审核备注
                com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsTemplateListResponseBody.Reason reasonBody = smsTemplateBody.getReason();
                if (!ObjectVerify.isNull(reasonBody)) {
                    QuerySmsTemplateListResponseModel.SmsTemplate.Reason reason = new QuerySmsTemplateListResponseModel.SmsTemplate.Reason();
                    if(!ObjectVerify.isNull(reasonBody.getRejectDate())) {
                        try {
                            reason.setRejectDate(DateConvert.stringToDate(reasonBody.getRejectDate(), DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                        } catch (BasicsException e) {
                            throw new AliYunConfigRuntimeException(e.getLocalizedMessage());
                        }
                    }
                    reason.setRejectInfo(reasonBody.getRejectInfo());
                    reason.setRejectSubInfo(reasonBody.getRejectSubInfo());

                    smsTemplate.setReason(reason);
                }
                return smsTemplate;
            }).collect(Collectors.toList());
            querySmsTemplateListResponseModel.setSmsTemplateList(smsTemplateList);
        }
        return querySmsTemplateListResponseModel;
    }

}
